package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Question;
import com.tanhua.dubbo.api.mongo.MongoQuestionApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(timeout = 10000)
public class MongoQuestionApiImpl implements MongoQuestionApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Question> findByQid(ObjectId qid) {
        Query query = new Query(Criteria.where("questionnaireId").is(qid));
        return mongoTemplate.find(query, Question.class);
    }

    @Override
    public void save(Question q1) {
        mongoTemplate.save(q1);
    }

    @Override
    public Question findById(ObjectId questionId) {
        return mongoTemplate.findById(questionId,Question.class);
    }
}
